import {combineReducers}  from 'redux'
import {QUERY_ALL_TOPNEWS,QUERY_ALL_NEWS_BYTYPE,QUERY_NEWS_DETAILS,ADD_NEWS,ADD_INFO,QUERY_ALL_INFO_BYFLOOR} from './action-types'
import { ListView } from 'antd-mobile'

function topNewsListData(state,action){
    state = state||{
        dataBlob:{},
        sectionIDs:[],
        rowIDs:[]
    }
    const getSectionData = (dataBlob, sectionID) => dataBlob[sectionID]
    const getRowData = (dataBlob, sectionID, rowID) => dataBlob[sectionID + ':' + rowID]

    const dataSource = new ListView.DataSource({
        getRowData,
        getSectionHeaderData: getSectionData,
        rowHasChanged: (row1, row2) => row1 !== row2,
        sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
    })
			
    switch(action.type){
        case QUERY_ALL_TOPNEWS :
            genData(state,action.payload)
            return {
                ...state,
                datasource:dataSource.cloneWithRowsAndSections(state.dataBlob,state.sectionIDs,state.rowIDs)
            }
        default:
            return {
                ...state,
                datasource:dataSource.cloneWithRowsAndSections(state.dataBlob,state.sectionIDs,state.rowIDs)
            }
    }
}

//根据传入的数据生成listview需要的数据结构dataBlob
function genData(state,data) {
    let sectionIDs = []
    let rowIDs = []
    let dataBlob = {}
    // 遍历数组中对应的数据并存入变量内
    for (let i = 0; i < data.length; i++) {
        // 将组号存入 sectionIDs 中
        sectionIDs.push(i)
        // 将每栏目标题内容存入 dataBlob 中
        dataBlob[i] = data[i].column
        // 取出该栏目所有的list
        const list = data[i].list
        rowIDs[i] = []
        // 遍历list
        for (let j = 0; j < list.length; j++) {
            // 设置标识
            rowIDs[i].push(j)
            // 根据标识,将数据存入 dataBlob
            dataBlob[i + ':' + j] = list[j]
        }
    }
    state.sectionIDs = sectionIDs
    state.rowIDs = rowIDs
    state.dataBlob = dataBlob
}


function newsListData(state,action){
    state = state||{
        dataBlob:[]
    }
    let dataSource = new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
    })
    let list = []
    switch(action.type){
        case ADD_NEWS :
            list = [...state.dataBlob]
            list.unshift(action.payload)
            return {
                dataBlob:list,
                datasource:dataSource.cloneWithRows(list)
            }
        case QUERY_ALL_NEWS_BYTYPE :
            if(action.payload.pageIndex>1){
                list = [...state.dataBlob,...(action.payload.newsList)]
            }else{
                list = [...(action.payload.newsList)]
            }
            return {
                dataBlob:list,
                datasource:dataSource.cloneWithRows(list)
            }
        default:
            return {
                ...state,
                datasource:dataSource.cloneWithRows(state.dataBlob)
            }
    }
}



function watchListData(state,action){
    state = state||{
        dataBlob:[]
    }
    let dataSource = new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
    })
    let list = []
    switch(action.type){
        case ADD_INFO :
            list = [...state.dataBlob]
            list.unshift(action.payload)
            return {
                dataBlob:list,
                datasource:dataSource.cloneWithRows(list)
            }
        case QUERY_ALL_INFO_BYFLOOR :
            list = [...(action.payload.watchList)]
            return {
                dataBlob:list,
                datasource:dataSource.cloneWithRows(list)
            }
        default:
            return {
                ...state,
                datasource:dataSource.cloneWithRows(state.dataBlob)
            }
    }
}


const initNewsDetail={
    id:'',
    title:'',
    date:'',
    type:'',
    content:''
}
function newsDetail(state=initNewsDetail,action){
    switch(action.type){
        case QUERY_NEWS_DETAILS :
            return action.payload
        default:
            return state
    }
}

const reducers = combineReducers({topNewsListData,newsListData,watchListData,newsDetail})
export default reducers